<?php
/**
 * 素材后台控制器
 *
 *
 * @author    lanhongjie <lanhj@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */

namespace App\Http\Controllers\Backstage;

use App\Services\ImgServices\ImgTool;
use App\Services\ProductTempService\ProductTempTool;
use App\Services\SourcePicService\SourcePicBakService;
use App\Services\SourcePicService\SourcePicTool;
use App\Services\TagService\TagService;
use App\Services\TagService\TagTool;
use App\Services\Tool\GeneralTool;
use App\Services\UserService\UserTool;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Response;
use App\Services\ModifyResourcePriceRecordsService\ModifyResourcePriceRecordsService;

class SourcePicBackController extends Controller
{
    protected $sourcePicBakSer;
    protected $sourcePicTool;
    protected $userTool;
    protected $tagSer;
    protected $modifyResourcePriceRecordsSer;
    /**
     * SourcePicBackController constructor.
     */
    public function __construct(SourcePicBakService $sourcePicBakSer,
                                SourcePicTool $sourcePicTool,
                                TagService $tagSer,
                                ModifyResourcePriceRecordsService $modifyResourcePriceRecordsService,
                                UserTool $userTool)
    {
        $this->sourcePicBakSer=$sourcePicBakSer;
        $this->sourcePicTool=$sourcePicTool;
        $this->userTool=$userTool;
        $this->tagSer=$tagSer;
        $this->modifyResourcePriceRecordsSer=$modifyResourcePriceRecordsService;

    }

    /**
     * 添加素材
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getAddSourcePic(Request $request)
    {
        return view('backstage.sourcepic.addSourcePic');
    }

    /**
     * 添加素材标签页面
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getAddSourcePicTag(Request $request)
    {
        return view('backstage.sourcepic.addTag');
    }

    /**
     * 更新素材标签页
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function updateSourcePicTagPage(Request $request)
    {
        $rule=array(
            'id'=>'required|integer|min:1',
        );
        $validator=SourcePicTool::store($request,$rule);
        $id=intval($request->input('id'));
        if ($validator->fails())
        {
            if($validator->errors()->first('id')){
                return redirect()->back()->with('error_message', '添加失败!');
            }
        }
        $data['tag_id']=$id;
        $data['app_id']=1;
        $tag=TagTool::getTagByTagId($data);
        if(empty($tag)){
            return redirect()->back()->with('error_message', '暂无数据!');
        }
        $has_binding_tag=TagTool::hasBindingTagByTagId($id,1);
        $p_tag_objs=array();
        if($has_binding_tag){
            $p_tags=TagTool::getTagIdsBySubTagId($id,1);
            foreach ($p_tags as $p_tag){
                $tag_name=TagTool::getTagNameByTagId($p_tag,1);
                if(!empty($tag_name)){
                    $p_tag_arr=array();
                    $p_tag_arr['tag_name']=$tag_name;
                    $p_tag_arr['tag_id']=$p_tag;
                    $p_tag_objs[]=(object)$p_tag_arr;
                }
            }
        }
        $tag->original_image=ImgTool::getImgUrlByImgPath($tag->original_image);
        $already_bound_tag_ids=TagTool::getTagIdsBySubTagId($id,1);
        $already_bound_tag_ids[]=$id;
        $un_bound_tags=$this->tagSer->searchResourceTagList('',1,1,$already_bound_tag_ids);
        $tag->has_sub_tag=$p_tag_objs;
        $tag->p_tags=$p_tag_objs;
        return view('backstage.sourcepic.updateTag')
            ->with('tag',$tag)
            ->with('un_bound_tags',$un_bound_tags);
    }
    /**
     * 批量上传素材页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getBulkAddSourcePic(Request $request)
    {
        $tag_id=intval($request->input('tag_id'));
        if(empty($tag_id)){
            return redirect()->back()->with('error_message', '编号有错!');
        }
        $tag_data['tag_id']=$tag_id;
        $data['app_id']=1;
        $tag=TagTool::getTagByTagId($tag_data);
        if(empty($tag)){
            return redirect()->back()->with('error_message', '标签不存在!');
        }elseif($tag->app_id!=1){
            return redirect()->back()->with('error_message', '不是素材标签!');
        }elseif($tag->tag_type==1){
            return redirect()->back()->with('error_message', '特殊素材标签!');
        }
        $data['tag_id']=$tag_id;
        $data['app_id']=1;
        $tag=TagTool::getTagByTagId($data);
        if(empty($tag)){
            return redirect()->back()->with('error_message', '标签不存在!');
        }
        $tag->tag_name=TagTool::getTagNameByTagId($tag_id,1);
        return view('backstage.sourcepic.bulkAddSourcePic')->with('tag_id',$tag_id)->with('tag',$tag);
    }

    /**
     * 获取素材列表
     * @param Request $request
     * @return $this
     */
    public function getSourcePicList(Request $request)
    {
        $audit_state=$request->input('audit_state');
        $is_shelves=$request->input('is_shelves');
        $platform=$request->input('platform');
        $audit_state=($audit_state===null)?-1:intval($audit_state);
        $is_shelves=($is_shelves===null)?-1:intval($is_shelves);
        $platform=($platform===null)?-1:intval($platform);
        $conditions['audit_state']=$audit_state;
        $conditions['is_shelves']=$is_shelves;
        $conditions['platform']=$platform;
        $source_pics=$this->sourcePicBakSer->getSourcePicList($conditions,10);
        if($source_pics===false){
            return redirect()->back()->with('error_message', '暂无数据!');
        }
        foreach ($source_pics as $source_pic){
            $source_pic->original_image=ImgTool::getImgUrlByImgPath($source_pic->original_image);
            $source_pic->thumbnail=ImgTool::getImgUrlByImgPath($source_pic->thumbnail);
            $source_pic->small_image=ImgTool::getImgUrlByImgPath($source_pic->small_image);
            $user_id=$source_pic->user_id;
            $user_name='';
            $userinfo=$this->userTool->userInfoByid($user_id);
            if(!empty($userinfo)){
                $user_name=$userinfo->nick_name;
            }
            $is_resource_modify_price=ModifyResourcePriceRecordsService::is_resource_modify_price($source_pic->source_pic_id,0,1);
            $has_modify_price=false;
            $modify_price=0;
            //是不是官方素材
            $is_official=false;
            $official_user_id=env('MYTEE_OFFICIAL_USER_ID');
            if ($is_resource_modify_price!==false){
                $has_modify_price=true;
                $modify_price=$is_resource_modify_price['modify_price'];
            }
            if($official_user_id==$source_pic->user_id){
                $is_official=true;
            }
            $source_pic->has_modify_price=$has_modify_price;
            $source_pic->modify_price=$modify_price;
            $source_pic->user_name=$user_name;
            $source_pic->is_official=$is_official;
        }
        return view('backstage.sourcepic.sourcePicList')
            ->with('source_pics',$source_pics)
            ->with('audit_state',$audit_state)
            ->with('is_shelves',$is_shelves)
            ->with('platform',$platform);
    }

    /***
     * 获取标签关联素材
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function getTagSourcePicMaps(Request $request){
        $v=array(
            'id'=>'required|integer|min:1',
        );
        $validator=ProductTempTool::store($request,$v);
        $id= intval($request->input("id"));
        if ($validator->fails())
        {
            if($validator->errors()->first('id')){
                return redirect()->back()->with('error_message', 'id获取出错!');
            }
        }
        $data['tag_id']=$id;
        $data['app_id']=1;
        $tag=TagTool::getTagByTagId($data);
        if(empty($tag)){
            return redirect()->back()->with('error_message', '标签不存在!');
        }
        $tag->tag_name=TagTool::getTagNameByTagId($id,1);
        $tag_resource_maps=$this->sourcePicBakSer->getSourcePicByTagId($id);
        foreach ($tag_resource_maps as $tag_resource_map){
            $tag_resource_map->small_image='';
            $tag_resource_map->user_id='';
            $tag_resource_map->audit_state='';
            $tag_resource_map->audit_value='';
            $tag_resource_map->is_shelves='';
            $tag_resource_map->is_show='';
            $source_pic=$this->sourcePicBakSer->getSourcePicSimpleInfoBySourcePicId($tag_resource_map->source_pic_id);
            if(!empty($source_pic)){
                $tag_resource_map->small_image=ImgTool::getImgUrlByImgPath($source_pic->small_image);
                $tag_resource_map->user_id=$source_pic->user_id;
                $tag_resource_map->audit_state=$source_pic->audit_state;
                $tag_resource_map->audit_value=$source_pic->audit_value;
                $tag_resource_map->is_shelves=$source_pic->is_shelves;
                $tag_resource_map->is_show=$source_pic->is_show;
            }
            $user_id=$source_pic->user_id;
            $user_name='';
            if($user_id!=0){
                $userinfo=$this->userTool->userInfoByid($user_id);
                if(!empty($userinfo)){
                    $user_name=$userinfo->nick_name;
                }
            }
            $tag_resource_map->user_name=$user_name;
        }
        return view('backstage.sourcepic.tagSourcepicMap')
            ->with('tag',$tag)
            ->with('tag_resource_maps',$tag_resource_maps);
    }
    /**
     * 获取素材对应的资源标签
     * @param Request $request
     * @return mixed
     */
     public function getSourcePicResourceTags(Request $request)
{
    $resource_tags=array();
    $resource_tag_ids=TagTool::getResourceTagIds(1,1);
    foreach ($resource_tag_ids as $resource_tag_id){
        $tag_arr=array();
        $tag_arr['tag_id']=$resource_tag_id;
        $tag_arr['tag_name']=TagTool::getTagNameByTagId($resource_tag_id,1);
        $resource_tags[]=(object)$tag_arr;
    }
    $result['state']=-1;
    $result['message']='成功';
    $result['data']=$resource_tags;
    return response()->json($result);
}

    /**
     * 批量更新素材信息
     * @param Request $request
     * @return mixed
     */
    public function bachUpdateSourcePic(Request $request)
    {
        $rule=array(
            'type'=>'required|integer|min:1',
            'type_value'=>'required|integer|min:1',
            'data'=>'required|array|min:1',
        );
        $validator=SourcePicTool::store($request,$rule);
        $type=intval($request->input('type'));
        $type_value=intval($request->input('type_value'));
        $data_arr=$request->input('data');
        if ($validator->fails())
        {
            if($validator->errors()->first('type')){
                $result['message']='类型格式错误';
            }
            if($validator->errors()->first('type_value')){
                $result['message']='类型值格式错误';
            }
            if($validator->errors()->first('data')){
                $result['message']='选中值数组格式错误';
            }
            $result['state']=-2;
            return response()->json($result);
        }
        $data['type']=$type;
        $data['type_value']=$type_value;
        $data['data_arr']=$data_arr;
        $res=$this->sourcePicBakSer->bachUpdateSourcePic($data);
        if($res){
            $result['message']='更新成功';
            $result['state']=1;
        }else{
            $result['message']='更新失败';
            $result['state']=-1;
        }
        return response()->json($result);
    }
    /**
     * 素材审核不通过
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function auditNotPass(Request $request)
    {
        $HTTP_REFERER=isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:"";
        $source_pic_id=trim($request->input('source_pic_id'));
        $source_pic_exist=$this->sourcePicBakSer->sourcePicExistBySourcePicId($source_pic_id,1);
        if(!$source_pic_exist){
            return redirect()->back()->with('error_message', '素材不存在!');
        }
        return view('backstage.sourcepic.AuditNotPassTheReason')
            ->with('source_pic_id',$source_pic_id)
            ->with('referer_url',$HTTP_REFERER);
    }

    /**
     * 保存审核不通过
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postAuditNotPass(Request $request)
    {
        $rule=array(
            'Msg'=>'required|string|',
            'source_pic_id'=>'required|string|',
        );
        $validator=SourcePicTool::store($request,$rule);
        $source_pic_id=trim($request->input('source_pic_id'));
        $msg=trim($request->input('Msg'));
        if ($validator->fails())
        {
            if($validator->errors()->first('Msg')){
                $result['message']='内容格式错误';
            }
            if($validator->errors()->first('source_pic_id')){
                $result['message']='素材编号格式错误';
            }
            $result['state']=-2;
            return response()->json($result);
        }
        $source_pic=$this->sourcePicBakSer->getSourcePic($source_pic_id);
        if(empty($source_pic)){
            $result['state']=-3;
            $result['message']='素材找不到';
        }
        if($source_pic->audit_state===3){
            $result['state']=1;
            $result['message']='修改成功';
        }
        if(isset($result)){
            return response()->json($result);
        }
        $res=$this->sourcePicBakSer->auditNotPass($source_pic_id,$msg);
        if($res){
            $result['state']=1;
            $result['message']='修改成功';
        }else{
            $result['state']=-1;
            $result['message']='修改失败';
        }
        return response()->json($result);
    }

    /**
     * 保存审核通过
     * @param Request $request
     * @return mixed
     */
    public function postAuditPass(Request $request)
    {
        $rule=array(
            'source_pic_id'=>'required|string|',
            'audit_state'=>'required|integer|min:1|max:3',
        );
        $validator=SourcePicTool::store($request,$rule);
        $source_pic_id=trim($request->input('source_pic_id'));
        $audit_state=intval($request->input('audit_state'));
        if ($validator->fails())
        {
            if($validator->errors()->first('source_pic_id')){
                $result['message']='素材编号格式错误';
            }
            if($validator->errors()->first('audit_state')){
                $result['message']='系统繁忙，请稍后再试';
            }
            $result['state']=-2;
            return response()->json($result);
        }
        $source_pic=$this->sourcePicBakSer->getSourcePic($source_pic_id);
        if(empty($source_pic)){
            $result['state']=-3;
            $result['message']='素材找不到';
        }
        $res=$this->sourcePicBakSer->auditPass($source_pic_id,$audit_state);
        if($res){
            $result['state']=1;
            $result['message']='修改成功';
        }else{
            $result['state']=-1;
            $result['message']='修改失败';
        }
        return response()->json($result);
    }

    /***
     * 素材显示
     * @param Request $request
     * @return mixed
     */
    public function changeShow(Request $request)
    {
        $rule=array(
            'source_pic_id'=>'required|string|',
            'is_show'=>'required|integer|min:0|max:1',
        );
        $validator=SourcePicTool::store($request,$rule);
        $source_pic_id=trim($request->input('source_pic_id'));
        $is_show=intval($request->input('is_show'));
        if ($validator->fails())
        {
            if($validator->errors()->first('source_pic_id')){
                $result['message']='素材编号格式错误';
            }
            if($validator->errors()->first('is_show')){
                $result['message']='系统繁忙，请稍后再试';
            }
            $result['state']=-2;
            return response()->json($result);
        }
        $source_pic=$this->sourcePicBakSer->getSourcePic($source_pic_id);
        if(empty($source_pic)){
            $result['state']=-3;
            $result['message']='素材找不到';
        }
        $res=$this->sourcePicBakSer->changeShow($source_pic_id,$is_show);
        if($res){
            $result['state']=1;
            $result['message']='修改成功';
        }else{
            $result['state']=-1;
            $result['message']='修改失败';
        }
        return response()->json($result);
    }
    /**
     * 添加单张素材
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function postAddASourcePic(Request $request)
    {
        $rule=array(
            'image'=>'required|image|',
            'name'=>'required|string|',
            'price'=>'required|numeric|min:0',
            'stock'=>'integer|min:0',
            'is_infinite_stock'=>'integer|min:0|max:1',
            'is_infinite_time'=>'integer|min:0|max:1',
        );
        $validator=SourcePicTool::store($request,$rule);
        $image=$request->file('image');
        $name=trim($request->input('name'));
        $description=trim($request->input('description'));
        $price=doubleval($request->input('price'));
        $preferential_price=doubleval($request->input('preferential_price'));
        $shelf_time=trim($request->input('shelf_time'));
        $preferential_shelf_time=trim($request->input('preferential_shelf_time'));
        $add_preferential=intval($request->input('add_preferential'));
        $stock=intval($request->input('stock'));
        $preferential_stock=intval($request->input('preferential_stock'));
        $is_infinite_stock=intval($request->input('is_infinite_stock'));
        $is_infinite_time=intval($request->input('is_infinite_time'));
        $data=array();
        $val_data['image']=$image;
        $val_data['name']=$name;
        $val_data['price']=$price;
        $val_data['stock']=$stock;
        $val_data['preferential_stock']=$preferential_stock;
        $val_data['is_infinite_stock']=$is_infinite_stock;
        $val_data['is_infinite_time']=$is_infinite_time;
        $data['is_infinite_time']=empty($is_infinite_time)?0:$is_infinite_time;
        $data['is_infinite_stock']=empty($is_infinite_stock)?0:$is_infinite_stock;
        $data['stock']=empty($stock)?0:round($stock);
        $data['preferential_stock']=empty($stock)?0:round($preferential_stock);
        $data['price']=empty($price)?0:$price;
        $data['preferential_price']=empty($price)?0:$preferential_price;
        $data['name']=empty($name)?'':$name;
        $data['image']=empty($image)?'':$image;
        $data['description']=empty($description)?'':$description;
        $data['add_preferential']=$add_preferential;
        if(!empty($shelf_time)){
            $time_arr=explode("-",$shelf_time);
            $begin_time=strtotime($time_arr[0]);
            $end_time  =strtotime($time_arr[1])+86400-1;
            $data['begin_time']=$begin_time;
            $data['end_time']=$end_time;
            $val_data['begin_time']=$begin_time;
            $val_data['end_time']=$end_time;
        }
        if(($add_preferential===1)&&!empty($preferential_shelf_time)){
            $time_arr=explode("-",$shelf_time);
            $preferential_begin_time=strtotime($time_arr[0]);
            $preferential_end_time  =strtotime($time_arr[1])+86400-1;
            $data['preferential_begin_time']=$preferential_begin_time;
            $data['preferential_end_time']=$preferential_end_time;
            $val_data['preferential_begin_time']=$preferential_begin_time;
            $val_data['preferential_end_time']=$preferential_end_time;
        }
        //判断用户是不是设计师，以及用户是不是存在
        $validator->after(function($validator) use ($val_data)
        {
            if(($val_data['is_infinite_stock']==0)&&($val_data['stock']<$val_data['preferential_stock'])){
                $validator->errors()->add('preferential_stock', '优惠库存大于实际库存');
            }
        });
        if ($validator->fails())
        {
            $request->flash();
            return redirect()->back()->withErrors($validator->errors())->withInput();
        }
        $res=$this->sourcePicBakSer->addSourcePic($data);
        if($res){
            return redirect()->back()->with('success_message', '添加成功!');
        }else{
            return redirect()->back()->with('error_message', '添加失败!');
        }
    }

    /**
     * dropzone 批量上传素材
     * @param Request $request
     * @return mixed
     */
    public function postUploadSourcePicSimpleInfo(Request $request)
    {
        $rule=array(
            'price'=>'required|numeric|min:0',
            'file' => 'required|mimes:png,gif,jpeg,jpg,bmp',
            'tag_id'=>'integer|min:0',
            'upload_type'=>'integer|min:0|max:1',
        );
        $validator=SourcePicTool::store($request,$rule);
        $image=$request->file('file');
        $tag_id=intval($request->input('tag_id'));
        $price=floatval($request->input('price'));
        $data=$request->all();
        $platform=intval($request->input('upload_type'));
        if ($validator->fails())
        {
            if($validator->errors()->first('price')){
                $result['message']='定价格式错误';
            }
            if($validator->errors()->first('file')){
                $result['message']='图片格式';
            }
            if($validator->errors()->first('tag_id')){
                $result['message']='请重新选择标签';
            }
            $result['state']=-2;
            return response()->json($result);
        }
        if($platform!==1){
            $result= GeneralTool::createResult('', 'M00001');
            return Response::json($result);
        }
        if(empty($tag_id)){
            $result= GeneralTool::createResult('', 'M00001');
            return Response::json($result);
        }
        $tag_data['tag_id']=$tag_id;
        $data['app_id']=1;
        $tag=TagTool::getTagByTagId($tag_data);
        if(empty($tag)){
            $result= SourcePicTool::createResult('标签不存在','', 'M00001');
            return Response::json($result);
        }elseif($tag->app_id!=1){
            $result= SourcePicTool::createResult('不是素材标签','', 'M00002');
            return Response::json($result);
        }elseif($tag->tag_type==1){
            $result= SourcePicTool::createResult('特殊素材标签','', 'M00003');
            return Response::json($result);
        }
        $tag_has_sub_tag=TagTool::hasSubTagByTagId($tag_id,1);
        if($tag_has_sub_tag){
            $result= SourcePicTool::createResult('不是资源标签','', 'M00004');
            return Response::json($result);
        }
        $source_pic['file']=$image;
        $source_pic['price']=$price;
        $source_pic['tag_id']=$tag_id;
        $source_pic['is_shelves']=1;
        $source_pic['audit_state']=2;
        $source_pic['audit_value']='已通过';
        $source_pic['platform']=1;
        $source_pic['user_id']=env('MYTEE_OFFICIAL_USER_ID');
        $result=$this->sourcePicBakSer->uploadSourcePicSimpleInfo($source_pic);
        if($result!==false){
            return Response::json(array('state'=>'M00000','message'=>'保存成功！','source_pic_id'=>$result));
        }else{
            return Response::json(array('state'=>'M00001','message'=>'保存失败！','source_pic_id'=>''));
        }

    }

    /**
     * 从批量上传素材页面到，批量添加素材详情
     * @param Request $request
     * @return $this
     */
    public function postUpdateSourcePicDetails(Request $request)
    {
        $source_pic_ids_str=trim($request->input('source_pic_ids'));
        $source_pic_ids=explode(",",$source_pic_ids_str);
        $source_pic_ids=array_unique($source_pic_ids);
        $available_source_pics=array();
        foreach ($source_pic_ids as $source_pic_id){
            $source_pic_exist=$this->sourcePicTool->sourcePicExistBySourcePicId($source_pic_id);
            if($source_pic_exist){
                $source_pic=$this->sourcePicTool->getSourcePicBySourcePicId($source_pic_id);
                if($source_pic!==null){
                    $available_source_pics[]=$source_pic;
                }
            }
        }
        foreach ($available_source_pics as $source_pic){
            $source_pic->original_image=ImgTool::getImgUrlByImgPath($source_pic->original_image);
            $source_pic->thumbnail=ImgTool::getImgUrlByImgPath($source_pic->thumbnail);
            $source_pic->small_image=ImgTool::getImgUrlByImgPath($source_pic->small_image);
        }
        return view('backstage.sourcepic.bulkAddSourcePic')->with('source_pics',$available_source_pics);
    }
    /**
     * 检查标签是不是已经创建过
     * @param Request $request
     * @return mixed
     */
    public function postCheckTagName(Request $request)
    {
        $rule=array(
            'tag_name'=>'required|string|',
        );
        $validator=SourcePicTool::store($request,$rule);
        $tag_name=trim($request->input('tag_name'));
        $val_data['tag_name']=$tag_name;
        $data=array();
        if($validator->errors()->first('tag_name')){
            if(!empty($val_data['tag_name'])){
                $result=array('state'=>-1,'message'=>'为空');
            }else{
                $result=array('state'=>-2,'message'=>'格式不正确');
            }
            return Response::json($result);
        }
        $res=$this->sourcePicBakSer->checkTagNameExist($tag_name);
        if($res){
            $result=array('state'=>1,'message'=>'已用');
        }else{
            $result=array('state'=>1,'message'=>'可用');
        }
        return Response::json($result);
    }

    /**
     * 获取素材标签列表
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function sourcePicTagList(Request $request)
    {
        $rule=array(
            'page'=>'required|integer|min:1',
            'per_page'=>'required|integer|min:1',
        );
        $validator=SourcePicTool::store($request,$rule);
        $page=intval($request->input('page'));
        $per_page=intval($request->input('per_page'));
        $redirect_url='/backstage/source-pic/source-pic-tag-list';
        if ($validator->fails())
        {
            if($validator->errors()->first('page')){
                if(!empty($page)){
                    return redirect($redirect_url)->with('error_message', '获取页数有问题!');
                }
            }
            if($validator->errors()->first('per_page')){
                $per_page=10;
            }
        }
        if($page>1000000){
            return redirect($redirect_url)->with('error_message', '获取页数有问题!');
        }
        $data['per_page']=$per_page;
        $tags=$this->sourcePicBakSer->getSourcePicTagList($data);
        if(empty($tags)){
            return redirect()->back()->with('error_message', '暂无数据!');
        }
        return view('backstage.sourcepic.sourcePicTagList')->with('tags',$tags);
    }
    /**
     *  素材删除
     * @param Request $request
     * @return mixed
     */
    public function delSourcePic(Request $request){
        $source_pic_id=trim($request->input('id'));
        $source_pic_exist=$this->sourcePicTool->sourcePicExistBySourcePicId($source_pic_id);
        if(!$source_pic_exist){
            $result['state']=-3;
            $result['message']='数据不存在';
            return response()->json($result);
        }
        $update_result=$this->sourcePicBakSer->delSourcePicBySourcePicId($source_pic_id);
        if($update_result){
            $result['state']=1;
            $result['message']='更新成功';
        }else{
            $result['state']=-1;
            $result['message']='更新失败';
        }
        return response()->json($result);
    }
    public function delTagResourceMap(Request $request){
        $id=intval($request->input('id'));
        $exist=TagTool::tagResourceMapIdExistById($id);
        if(!$exist){
            $result['state']=-3;
            $result['message']='数据不存在';
            return response()->json($result);
        }
        $delete_result=$this->tagSer->deleteTagTempMapById($id);
        if($delete_result){
            $result['state']=1;
            $result['message']='删除成功';
        }else{
            $result['state']=-1;
            $result['message']='删除失败';
        }
        return response()->json($result);
    }

    /***
     * @param Request $request
     * @return mixed
     */
    public function addTagSourcePicMap(Request $request){
        $rule=array(
            'tag_id'=>'required|integer|min:1',
            'resource_id'=>'required|string|min:1',
        );
        $tag_id=intval($request->input('tag_id'));
        $resource_id=trim($request->input('resource_id'));
        $validator=ProductTempTool::store($request,$rule);
        $data['tag_id']=$tag_id;
        $data['resource_id']=$resource_id;
        if ($validator->fails())
        {
            if($validator->errors()->first('tag_id')){
                $result['state']=-2;
                $result['message']=' 标签编号格式错误';
            }
            if($validator->errors()->first('resource_id')){
                $result['state']=-4;
                $result['message']='素材编号格式错误';
            }
            if(!empty($result)){
                return response()->json($result);
            }
        }
        $tag_data['tag_id']=$tag_id;
        $tag_data['app_id']=1;
        $tag=TagTool::getTagByTagId($tag_data);
        if(empty($tag)){
            $result['state']=-5;
            $result['message']='标签不存在';
            return response()->json($result);
        }elseif ($tag->tag_type==1){
            $result['state']=-6;
            $result['message']='当前标签绑定范围是全部';
            return response()->json($result);
        }
        $exist=TagTool::resourceIdHasBindingTagId($tag_id,1,$resource_id,1);
        if($exist){
            $result['state']=1;
            $result['message']='保存成功';
            return response()->json($result);
        }
        $res=$this->sourcePicBakSer->addTagTempMap($data);
        if($res){
            $result['state']=1;
            $result['message']='保存成功';
        }else{
            $result['state']=-1;
            $result['message']='保存失败';
        }
        return response()->json($result);
    }

    /***
     * 标签下资源排序
     * @param Request $request
     * @return mixed
     */
    public function updateTagTempMapSort(Request $request){
        $id=intval($request->input('id'));
        $sort=intval($request->input('sort'));
        if(empty($sort)||!is_int($sort)){
            $result['state']=-2;
            $result['message']='排序数字格式错误';
            return response()->json($result);
        }
        $exist=TagTool::tagResourceMapExistById($id,1);
        if(!$exist){
            $result['state']=-3;
            $result['message']='数据不存在';
            return response()->json($result);
        }
        $update_result=$this->tagSer->updateTagResourceMapSort($id,$sort);
        if($update_result){
            $result['state']=1;
            $result['message']='更新成功';
        }else{
            $result['state']=-1;
            $result['message']='更新失败';
        }
        return response()->json($result);
    }

    /***
     * 更新素材名
     * @param Request $request
     * @return mixed
     */
    public function updateSourcePicName(Request $request){
        $rule=array(
            'source_pic_name'=>'|string|min:0',
            'id'=>'required|string|',
        );
        $validator=SourcePicTool::store($request,$rule);
        $id=trim($request->input('id'));
        $source_pic_name=trim($request->input('source_pic_name'));
        if ($validator->fails())
        {
            if($validator->errors()->first('source_pic_name')){
                $result['state']=-2;
                $result['message']='名字格式错误';
                return response()->json($result);
            }
            if($validator->errors()->first('id')){
                $result['state']=-3;
                $result['message']='素材编号格式出错';
                return response()->json($result);
            }
        }
        $source_pic=$this->sourcePicTool->getSourcePicBySourcePicId($id,1);
        if(empty($source_pic)){
            $result['state']=-3;
            $result['message']='数据不存在';
            return response()->json($result);
        }
        $official_user_id=env('MYTEE_OFFICIAL_USER_ID');
        if($source_pic->user_id!=$official_user_id){
            $result['state']=-4;
            $result['message']='当前素材不是官方素材！';
            return response()->json($result);
        }
        $data['source_pic_id']=$id;
        $data['name']=$source_pic_name;
        //价格要定时修改
        $update_result=$this->sourcePicBakSer->updateSourcePicDB($data);
        if($update_result){
            $result['state']=1;
            $result['message']='更新成功';
        }else{
            $result['state']=-1;
            $result['message']='更新失败';
        }
        return response()->json($result);
    }

    /***
     * 更新后台上传素材价格
     * @param Request $request
     * @return mixed
     */
    public function updatePrice(Request $request){
        $rule=array(
            'price'=>'required|numeric|min:0',
            'id'=>'required|string|',
        );
        $validator=SourcePicTool::store($request,$rule);
        $id=trim($request->input('id'));
        $price=floatval($request->input('price'));
        if ($validator->fails())
        {
            if($validator->errors()->first('price')){
                $result['state']=-2;
                $result['message']='单价格式出错';
                return response()->json($result);
            }
            if($validator->errors()->first('id')){
                $result['state']=-3;
                $result['message']='素材编号格式出错';
                return response()->json($result);
            }
        }
        $source_pic=$this->sourcePicTool->getSourcePicBySourcePicId($id,1);
        if(empty($source_pic)){
            $result['state']=-3;
            $result['message']='数据不存在';
            return response()->json($result);
        }
        $official_user_id=env('MYTEE_OFFICIAL_USER_ID');
        if($source_pic->user_id!=$official_user_id){
            $result['state']=-4;
            $result['message']='当前素材不是官方素材！';
            return response()->json($result);
        }
        $data['source_pic_id']=$id;
        $data['price']=$price;
        //价格要定时修改
//        $update_result=$this->sourcePicBakSer->updateSourcePicDB($data);
        $input['modify_price']=$price;
        $input['price']=$source_pic->price;
        $input['resource_type']=1;
        $input['resource_id']=$id;
        $input['admin_id']=Auth::user()->id;
        $input['admin_email']=Auth::user()->email;
        @$update_modify_price_result=$this->modifyResourcePriceRecordsSer->save_modify_resource_price_record($input);
        $result['state']=1;
        $result['message']='更新成功';
        return response()->json($result);
    }
}
